package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author RenBoQing
 * @date 2024年09月22日 10:59
 * @Description 微信小程序会员业务管理控制层
 */
@Api(tags = "微信小程序会员接口管理")
@RequestMapping("/p/user")
@RestController
public class MemberController {
    @Autowired
    private MemberService memberService;

    /*
     * 更新小程序会员的头像和昵称
     * @author RenBoQing
     * @date 2024/9/22 12:27
     * @param member
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("更新小程序会员的头像和昵称")
    @PutMapping("/setUserInfo")
    public Result<String> modifyMemberInfo(@RequestBody Member member) {
        boolean result = memberService.modifyMemberInfoByOpenId(member);
        return Result.handle(result);
    }

    /*
     * 查询当前小程序用户是否绑定手机号
     * @author RenBoQing
     * @date 2024/9/22 12:28
     * @return com.powernode.model.Result<java.lang.Boolean>
     */
    @ApiOperation("查询当前小程序用户是否绑定手机号")
    @GetMapping("/isBindPhone")
    public Result<Boolean> loadMemberIsBindPhone() {
        // 获取会员的openid
        String memberOpenId = AuthUtils.getMemberOpenId();
        // 根据会员openid 查询会员详情
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .eq(Member::getOpenId, memberOpenId)
        );
        // 返回当前微信小程序用户 是否绑定手机号
        return Result.success(StringUtils.hasText(member.getUserMobile()));
    }
}
